<template>
          <div class="connection-info">
            <div class="connection-icon">
              <icon-computer class="computer-icon" />
              <div class="data-line" :class="{ active: connectionStatus }">
                <div class="data-dot upload"></div>
                <div class="data-dot download"></div>
              </div>
              <icon-cloud class="vpn-icon" />
            </div>
            
            <a-button type="primary" class="connect-btn" @click="toggleConnection">
              {{ connectionStatus ? 'Disconnect' : `Connect ${selectConnection?.name}` }}
            </a-button>
          </div>

</template>


<script setup lang="ts">
import { ref, watch } from 'vue';
import { App } from '../../../bindings/slapp3/backend';
import { Profile } from '../../../bindings/slapp3/backend/application/domain';

const connectionStatus = ref(false);
const selectConnection = ref<Profile | undefined>(undefined);
const selectConnectionId = defineModel<number|undefined>('selectedConnectionId', {
  default: undefined,
});

const connect = async () => {
  if (selectConnection.value) {
    try {
      await App.Connect(selectConnection.value.id);
      connectionStatus.value = true;
    } catch (error) {
      console.error('Failed to connect:', error);
    }
  } else {
    console.warn('No connection selected');
  }
};

watch(() => selectConnectionId.value, async (newId) => {
  if (newId !== undefined) {
    try {
      const result = await App.GetConnect(newId);
      selectConnection.value = result;

    } catch (error) {
      console.error('Failed to get connection details:', error);
    }
  }
}, { immediate: true });

const toggleConnection = () => {
  connect()
  connectionStatus.value = !connectionStatus.value;
};

</script>

<style lang="less" scoped>

.connection-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  padding: 20px 10px;
}

.connection-icon {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  width: 100%;
  margin-bottom: 20px;
  
  .computer-icon, .vpn-icon {
    width: 48px;
    height: 48px;
    color: #4a90e2;
  }
}

.data-line {
  position: relative;
  width: 150px;
  height: 3px;
  background-color: #e0e0e0;
  margin: 0 15px;
  border-radius: 1.5px;
  overflow: hidden;
}

.data-line.active {
  background-color: #d0d0d0;
}

.data-line .data-dot {
  position: absolute;
  width: 10px;
  height: 3px;
  border-radius: 1.5px;
  opacity: 0;
}

.data-line.active .data-dot.upload {
  background-color: #4a90e2;
  animation: upload 1.5s infinite ease;
}

.data-line.active .data-dot.download {
  background-color: #34a853;
  animation: download 1.5s infinite ease;
  animation-delay: 0.75s;
}

@keyframes upload {
  0% {
    left: 0;
    opacity: 1;
  }
  100% {
    left: calc(100% - 10px);
    opacity: 1;
  }
}

@keyframes download {
  0% {
    right: 0;
    opacity: 1;
  }
  100% {
    right: calc(100% - 10px);
    opacity: 1;
  }
}

.info-items {
  width: 100%;
  margin: 20px 0;
}

.info-item {
  display: flex;
  margin-bottom: 15px;
  
  .info-label {
    width: 140px;
    text-align: right;
    padding-right: 10px;
    color: #666;
  }
  
  .info-value {
    font-weight: 500;
  }
}
.connect-btn {
  width: 200px;
  height: 40px;
  margin-top: 20px;
}
</style>